﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sunboxsoft.Common;
using System.Diagnostics;

namespace Sunboxsoft.WinForm.Shell.Common
{
    /// <summary>
    /// 读取配置信息
    /// </summary>
    public static class ConfigLoader
    {
        public static bool Load()
        {
            Config.AppSettings = new AppSettings();
            //Config.AppSettings.WebAppBaseUrl = System.Configuration.ConfigurationManager.AppSettings["APMS.WebApp.BaseUrl"];
            //if (string.IsNullOrEmpty(Config.AppSettings.WebAppBaseUrl))
            //{
            //    return false;
            //}

            Config.AppSettings.Product = System.Configuration.ConfigurationManager.AppSettings["Sunboxsoft.Product"];
            if (string.IsNullOrEmpty(Config.AppSettings.Product))
            {
                return false;
            }

            UIContext.Current.Config = new Config();

            UIContext.Current.Config.StorageDirectory = string.Format("{0}SunboxSoft\\{1}\\Client\\", GetSystemDrive(), Config.AppSettings.Product);
            UIContext.Current.Config.StorageLogDirectory = string.Format("{0}Logs\\", UIContext.Current.Config.StorageDirectory);


            if (!System.IO.Directory.Exists(UIContext.Current.Config.StorageDirectory))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(UIContext.Current.Config.StorageDirectory);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                    return false;
                }
            }

            Logger.InitByLogDirectory(UIContext.Current.Config.StorageLogDirectory);

            Logger.InfoToTag("Shell", string.Format("logger init on \"{0}\"", UIContext.Current.Config.StorageLogDirectory));

            //string configJsonString = HttpUtil.GetString(Config.GetConfigUrl());
            //if(string.IsNullOrEmpty(configJsonString))
            //{
            //    return false;
            //}

            //try
            //{
            //    Config.Current = JsonConfigUtil.LoadFromString<Config>(configJsonString);
            //}
            //catch (Exception ex)
            //{
            //    return false;
            //}
            return true;
        }

        private static string GetSystemDrive()
        {
            System.IO.DirectoryInfo dirWindows = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.System));

            System.IO.DirectoryInfo dirRoot = null;
            System.IO.DirectoryInfo dirParent = dirWindows.Parent;
            while (dirParent != null)
            {
                dirRoot = dirParent;
                dirParent = dirParent.Parent;
            }
            return dirRoot.FullName;
        }

        //class HttpUtil
        //{
        //    public static string GetString(string url)
        //    {
        //        try
        //        {
        //            HttpClient client = new HttpClient();
        //            return client.GetStringAsync(url).Result;
        //        }
        //        catch (Exception ex)
        //        {

        //        }
        //        return null;
        //    }
        //}
    }
}
